package com.juyu.miliao.api.service;

import com.alibaba.fastjson.JSONArray;
import com.juyu.miliao.api.cache.GradeCache;
import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.dao.redis.LineTimeRedisDao;
import com.juyu.miliao.api.dao.redis.UserCallRedisDao;
import com.juyu.miliao.api.domain.Grade;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.util.GradeUtil;
import com.juyu.miliao.common.util.SumAgeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static java.util.stream.Collectors.toList;


/**
 * by: 卜加超
 */
@Service
public class HostLineService {
    @Resource
    private LineTimeRedisDao lineTimeRedisDao;
    @Resource
    private UserInfoDao userInfoDao;
    @Resource
    private UserCallRedisDao userCallRedisDao;

    /**
     * 获取在线女主播
     */
    public List<User> getHostLine() {
        //只要女主播
        Set<String> hostLine = lineTimeRedisDao.getHostLineBySex(2);
        if(hostLine.size()==0){
            return new ArrayList<>();
        }
        List<Long>  hostUserIds = hostLine.stream().map(Long::parseLong).collect(toList());
        List<User> hostUserList = userInfoDao.getQualityHostByUserIds(hostUserIds);
        List<User>  temp= new ArrayList<>(hostUserList);
        if(hostUserList.size()>60){
            temp = new ArrayList<>(hostUserList.subList(0,60));
        }
        //存放免费女主播
        List<User> listFree = new ArrayList<>(temp.size()/2);
        //存放收费女主播
        List<User> listCharge = new ArrayList<>(temp.size()/2);
        for (User hostUser:temp){
            if(hostUser.getVideoPrice().equals("0") && hostUser.getIsVideo()!=2){
                hostUser.setCallType(2);
                listFree.add(hostUser);
                //&& hostUser.getIsCall()!=2
            }else if(hostUser.getChatPrice().equals("0")){
                hostUser.setCallType(1);
                listFree.add(hostUser);
            }else{
                // || hostUser.getIsCall()!=2
                if (hostUser.getIsVideo()!=2 ) {
                    hostUser.setCallType(0);
                    listCharge.add(hostUser);
                }
            }
        }
        //免费女主播在前，收费女主播在后
        listFree.addAll(listCharge);
        return listFree;
    }

    /**
     *免费聊
     */
    public User getHostLineByFree(long userId) {
        User user = userInfoDao.getUserById(userId);
        if(user.getSex()==2){
//            throw new ConsumeRuntimeException("没有在线免费男主播");
            return null;
        }
        int sex = 1;
        if(user.getSex()==1){
            sex = 2;
        }
        //获取异性主播
        Set<String> hostLine = lineTimeRedisDao.getHostLineBySex(sex);
        if(hostLine.size()==0){
            return null;
        }
        List<Long>  hostUserIds = hostLine.stream().map(Long::parseLong).collect(toList());
        //语音或视频免费的主播
        List<User> hostUserList = userInfoDao.getHostLineByFree(hostUserIds);
        if(hostUserList.size()==0){
            return null;
        }
        List<User> tempUserList = new ArrayList<>(hostUserList.size());
        for (User hostUser :hostUserList){
            if(hostUser.getVideoPrice().equals("0") && hostUser.getIsVideo()!=2){
                hostUser.setCallType(2);
                tempUserList.add(hostUser);
               // && hostUser.getIsCall()!=2
            }else if(hostUser.getChatPrice().equals("0")){
                hostUser.setCallType(1);
                tempUserList.add(hostUser);
            }
        }
        if(tempUserList.size()==0){
            return null;
        }
        List<User> returnList = new ArrayList<User>(tempUserList.size());
        for (User temp:tempUserList) {
            if(!userCallRedisDao.findHostCall(temp.getUserId()+"")){
                returnList.add(temp);
            }
        }
        if(returnList.size()==0){
            return null;
        }
        if(returnList.size()==1){
            return returnList.get(0);
        }
        user = returnList.get((int) (Math.random() * returnList.size()));
        // 魅力等级
        List<Grade> gradeList = GradeCache.INSTANCE.getGradeList(1);
        user.setmGrade(GradeUtil.getGradeCount(gradeList, user.getmGradeSum()));
        user.setmNextGradeSum(GradeUtil.getGradeNextSum(gradeList, user.getmGradeSum()));
        // 土豪等级
        gradeList = GradeCache.INSTANCE.getGradeList(2);
        user.setvGrade(GradeUtil.getGradeCount(gradeList, user.getvGradeSum()));
        user.setvNextGradeSum(GradeUtil.getGradeNextSum(gradeList, user.getvGradeSum()));
        // 生日转换成年龄
        if (user.getBirthday() != null) {
            user.setAge(SumAgeUtil.getAgeByBirth(user.getBirthday()));
            user.setBirthdayInstant(user.getBirthday().toString());
        }
        return user;

    }

    public JSONArray getHostLineByFreeIcon(long userId) {
        User user = userInfoDao.getUserById(userId);
        int sex = 1;
        if(user.getSex()==1){
            sex = 2;
        }
        JSONArray list = new JSONArray();
        if(sex==1){
            for(int i =1;i<54;i++){
                String s = "http://zb-web.oss-cn-hangzhou.aliyuncs.com/tupian/nan/a"+i+".jpg";
                list.add(s);
            }
        }else if(sex==2){
            for(int j =1;j<69;j++){
                String s = "http://zb-web.oss-cn-hangzhou.aliyuncs.com/tupian/nv/a"+j+".jpg";
                list.add(s);
            }
        }
        return list;
    }
}
